layui.config({
    base: baseUrl + '/js/layui/module/'
}).extend({//设定模块别名
    treeSelect: 'treeSelect/treeSelect',//static/js/layui/module/treeSelect/treeSelect.js
    formSelects: 'formSelects/formSelects-v4',//static/js/layui/module/formSelects/formSelects-v4.js
    dtree : 'dtree/dtree'
});
layui.use(['form','layer','laydate','table','laytpl','element','treeSelect','formSelects','dtree'],function(){
    var element = layui.element;
    var laydate = layui.laydate;
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
    var treeSelect = layui.treeSelect;
    var formSelects = layui.formSelects;
    var dtree = layui.dtree;

    var tableIns = table.render({
        elem: '#callListTable' //初始化使用dom id
        ,url : 'queryCallHistoryList?sort=desc&callTaskId='+$("#callTaskId").val()
        ,id : "callListTable"
        ,loading: true
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[ //标题栏
            {field: 'customerName', title: '客户名称', align:'center',width:'15%',minWidth:150},
            {field: 'remark', title: '通话状态', align:'center',width:'10%',minWidth:120,templet:function (d) {

                /*var recordTimeLength=d.recordTimeLength

                if(isNaN(recordTimeLength)){
                    return "未接通"
                }else{
                    if(recordTimeLength>0){

                        return "已接通"
                    }
                    return "未接通"
                }*/
                if("8"==d.commitTo||"17"==d.commitTo){//为freeswitch外呼

                    if(d.targetCallAnswered==null&&d.recordTimeLength>0){
                        return "未知"
                    }
                    if(d.targetCallAnswered){
                        return "已接通"
                    }else{
                        return "未接通"
                    }
                }else{
                    if(d.mainCallAnswered==null&&d.recordTimeLength>0){
                        return "未知"
                    }
                    if(d.mainCallAnswered){
                        return "已接通"
                    }else{
                        return "未接通"
                    }
                }

            }},
            {field: 'createTime', title: '外呼时间', align:'center',width:'20%',minWidth:250},
            {field: 'recordTimeLength', title: '通话时长', align:'center',width:'10%',minWidth:120,templet:function (d) {

                var recordTimeLength=d.recordTimeLength;
                if (isNaN(recordTimeLength)){
                    return "0秒";
                }else{
                  /*  var minutes= Math.floor(recordTimeLength/60);
                    var second= recordTimeLength%60;
                    return minutes+"分"+second+"秒";*/
                    var hour = Math.floor(recordTimeLength / (60 * 60));
                    var min = (Math.floor(recordTimeLength / (60))  - hour * 60);
                    var sec = (recordTimeLength-hour*60*60-min*60);
                    if(hour != 0){
                        return hour+"时"+min+"分"+sec+"秒";
                    }else{
                        if(min == 0){
                            return sec+"秒";
                        }else{
                            return min+"分"+sec+"秒";
                        }
                    }
                }
            }},
            /*{field: 'callUser', title: '外呼人', align:'center',width:'10%',minWidth:120,templet:function (d) {
                return "李阿宝"
            }},*/
            {field: 'commitTo', title: '外呼线路', align:'center',width:'15%',minWidth:150,templet:function (d) {
                if(d.commitTo!=null){
                    if(d.commitTo == "INT_ALIYUN"){
                        return "双呼";
                    }else if(d.commitTo == "ASR_ALIYUN"){
                        return "单呼";
                    }else if(d.commitTo == "FREESWITCH"){
                        return "freeswitch外呼";
                    }else{
                        return "";
                    }
                }else{
                    return "";
                }
            }},
            {field: 'mainCallPhone', title: '客户号码', align:'center',width:'15%',minWidth:150},
            {field: 'sessionId', title: '通话id', align:'center',width:'20%',minWidth:150},
            {field: 'customerNeedLevel', title: '意向等级', align:'center',width:'10%',minWidth:120,},
            {title: '操作',templet:'#barDemo',align:"center",width:'10%',event: 'showDetail',minWidth:120}
        ]]
        // ,size:'sm'//小尺寸
        ,page: {
            limit: 10
            ,curr: 1
            ,groups: 5
            ,theme: '#5FB878'
            ,first: '首页'
            ,last: '尾页'
            ,prev: '<em><i class="layui-icon">&#xe603;</i></em>'
            ,next: '<em><i class="layui-icon">&#xe602;</i></em>'
            ,layout: [ 'prev', 'page', 'next', 'skip','count']
        }
        ,done: function(res, curr, count){
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            console.log(res);
            //得到当前页码
            console.log(curr);
            //得到数据总量
            console.log(count);
            intTableColorChange();
            LayUIDataTable.SetJqueryObj($);// 第一步：设置jQuery对象
            LayUIDataTable.HideField('sessionId');// 隐藏列-单列模式
            //LayUIDataTable.HideField(['num','match_guest']);// 隐藏列-多列模式
            var currentRowDataList = LayUIDataTable.ParseDataTable(function (index, currentData, rowData) {
                console.log("当前页数据条数:" + currentRowDataList.length);
                console.log("当前行索引：" + index);
                console.log("触发的当前行单元格：" + currentData);
                console.log("当前行数据：" + JSON.stringify(rowData));
                console.log(rowData.id);
            });
        },text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
    });

    //监听单元格事件
    table.on('tool(callListTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'view'){
            var callWin = parent.layer.open({
                title : "查看任务详情",
                type : 2,
                content : [baseUrl+"/linkageCall/viewCallDetail?sessionId="+data.sessionId+"&commitTo="+data.commitTo],//
                resize:true,
                maxmin:false,
                isOutAnim:true,
                scrollbar:true,
                area : ['80%', '93%']
            });
            setTimeout(function(){
                parent.layer.tips('点击关闭，返回上个页面', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            },500);
            parent.layer.full(callWin);
        }
    });

    var receptionStatusChart = echarts.init(document.getElementById('receptionStatus'));
    var talkTimeChart = echarts.init(document.getElementById('talkTime'));
    var callTurnsChart = echarts.init(document.getElementById('callTurns'));
    var customerLevelChart = echarts.init(document.getElementById('customerLevel'));

    loadEchartData("接听状况比例",receptionStatusPieVos,receptionStatusChart,"macarons");
    loadEchartData("通话时长比例",talkTimePieVos,talkTimeChart,"infographic");
    loadEchartData("通话轮次比例",callTurnsPieVos,callTurnsChart,"shine");
    loadEchartData("客户等级比例",customerLevelPieVos,customerLevelChart,"macarons2");
/*
    var option = {
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c}个 ",
            /!*position: ['40%', '50%']
            ,textStyle:{fontSize :9}*!/
        },
        series: [
            {
                name:'接听状况比例',
                type:'pie',
                radius: ['40%', '55%'],
                avoidLabelOverlap: true,//是否启用防止标签重叠策略
                label: {
                    normal: {
                        formatter: function(data) {
                            console.log(data);
                            return  '{name|' + data.name + '}'+'\n{red|' + data.value + '}{gray|个}';
                        },
                        rich: {
                            name: {
                                fontWeight: 'bold'
                            },
                            gray: {
                                color: 'gray'

                            },
                            red: {
                                color: 'red'
                            }
                        }
                    }
                },
                data:receptionStatusPieVos
            }
        ]
    };*/
});

//选中行时，高亮显示
function intTableColorChange(){
    $(".layui-table-body tr").click(function(e){
        $(".layui-table-body  tr ").css("background","");
        $(this).css("background","#e6e6e6");
    });
}


/**
 * title 标题
 * dataList 数据合集 [默认 name 类型名称、 value 该类型在图标中的含量]
 * loadChar 装载到哪个图表中
 * */
function loadEchartData(title,dataList,loadChar,theme) {
    var unit = "个";
    console.log(loadChar);
    if("接听状况比例" != title){
        unit = "%";
    }
    var option = {
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c}"+unit+" ",
            /*position: ['40%', '50%']
            ,textStyle:{fontSize :9}*/
        },
        series: [
            {
                name:title,
                type:'pie',
                radius: ['40%', '60%'],
                avoidLabelOverlap: true,//是否启用防止标签重叠策略
                minAngle:'15',
                label: {
                    normal: {
                        formatter: function(data) {
                            return  '{name|' + data.name + '}'+'\n{red|' + data.value + '}{gray|'+unit+'}';
                        },
                        rich: {
                            name: {
                                fontWeight: 'bold'
                            },
                            gray: {
                                color: 'gray'

                            },
                            red: {
                                color: 'red'
                            }
                        }
                    }
                },
                data:dataList
            }
        ]
    };
    // function statusPersentClick(param) {
    //     toCustomer('/crmCustomer/page/queryCustomer?status='+param.data.dataValue,param.data.name);
    // }
    // 使用刚指定的配置项和数据显示图表。
    loadChar.setOption(option);
    // receptionStatusChart.on("click", statusPersentClick);
}




